﻿using Coldairarrow.Entity.MEGA;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.MEGA
{
    public class MEGA_WorkerBusiness : BaseBusiness<MEGA_Worker>, IMEGA_WorkerBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public MEGA_WorkerBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<MEGA_Worker>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<MEGA_Worker>();
            var search = input.Search;

            //筛选
            //if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            //{
            //    var newWhere = DynamicExpressionParser.ParseLambda<MEGA_Worker, bool>(
            //        ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
            //    where = where.And(newWhere);
            //}

            if (!search.Keyword.IsNullOrEmpty())
            {
                q = q.Where(e => e.Name.Contains(search.Keyword) || e.Phone.Contains(search.Keyword) || e.Code.Contains(search.Keyword));
            }
            q = q.OrderBy(input.SortField, input.SortType).Where(where);

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<MEGA_Worker> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(MEGA_Worker data)
        {
            data.CreatorId = _operator.UserId;
            var list = GetIQueryable().Where(x => x.Code.Equals(data.Code)).ToList();
            if (list.Count > 0)
            {
                throw new System.Exception("该员工已存在!");
            }
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(MEGA_Worker data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}